package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.gxuwz.medical.dao.AreaDao;
import com.gxuwz.medical.dao.ChronicdisDao;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.domain.chronicdis.Chronicdis;

public class ChronicdisServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m=req.getParameter("m");
	    if("list".equals(m)){
	      process(req, resp, "/page/chronicdis/chronicdis_list.jsp");
	    }else if("input".equals(m)){
		  process(req, resp, "/page/chronicdis/chronicdis_add.jsp");
		}else if("get".equals(m)){
			  process(req, resp, "/page/chronicdis/chronicdis_edit.jsp");
		}else if("add".equals(m)){
			add(req, resp);
		}else if("edit".equals(m)){
			edit(req, resp);
		}else if("del".equals(m)){
			del(req, resp);
		}else{
			error(req,resp);
		}
	
	}
	private void groupList(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String illCode = req.getParameter("illCode");
		ChronicdisDao chronicdisDao =new ChronicdisDao();
		try{
			List<Chronicdis>chronicdisList=chronicdisDao.findChildChronicdis(illCode);
			String respBody = JSON.toJSONString(chronicdisList);
			resp.setCharacterEncoding("utf-8");
			PrintWriter out = resp.getWriter();
			System.out.println("respBody:"+respBody);
			out.print(respBody);
			out.flush();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	private void add(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
  	  String illCode=req.getParameter("illCode");
  	  String illName=req.getParameter("illName");
  	  String pyCode=req.getParameter("pyCode");
  	  String wbCode=req.getParameter("wbCode");
  	  Chronicdis chronicdis=new Chronicdis(illCode, illName, pyCode, wbCode);
  	  try{
  		chronicdis.add();
  		process(req, resp, "/page/chronicdis/chronicdis_list.jsp");
  	  }catch(Exception e){
  		e.printStackTrace();
  	  }
	
  
	}
	private void edit(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
	  	  String illCode=req.getParameter("illCode");
	  	  String illName=req.getParameter("illName");
	  	  String pyCode=req.getParameter("pyCode");
	  	  String wbCode=req.getParameter("wbCode");
	  	  Chronicdis chronicdis=new Chronicdis(illCode, illName, pyCode, wbCode);
	  	  try{
	  		chronicdis.edit();
	  		process(req, resp, "/page/chronicdis/chronicdis_list.jsp");
	  	  }catch(Exception e){
	  		e.printStackTrace();
	  	  }
		}
	private void del(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
	  	  String illCode=req.getParameter("illCode");
	  	  Chronicdis chronicdis=new Chronicdis();
	  	  try{
	  		System.out.println(illCode);
	  		chronicdis.del(illCode);
	  		process(req, resp, "/page/chronicdis/chronicdis_list.jsp");
	  	  }catch(Exception e){
	  		e.printStackTrace();
	  	  }
	  
		}

}
